<?php

namespace ManiaLivePlugins\MLEPP\Widgets\Widgets\Controlers\Defaults\MessageWindow;

use ManiaLivePlugins\MLEPP\Widgets\Structures\TypeController;
use ManiaLive\PluginHandler\PluginHandler;
use ManiaLive\Event\Dispatcher;
use ManiaLive\Data\Storage;
use ManiaLive\Gui\Windowing\CustomUI;
use ManiaLive\Gui\Windowing\WindowHandler;

class Standard extends TypeController {

    public function onInit() {
        parent::onInit();
        $this->setVersion(1);
        $this->setWhenToShow(0);
        $this->setPublicMethod('sendMessageToTab');
    }

    public function onLoad() {
        $this->enableStorageEvents();
        $this->enableDedicatedEvents();
        $mlepp = \ManiaLivePlugins\MLEPP\Core\Mlepp::getInstance();
        $redirect = array("Karma", "Rankings", "LocalRecords", "ManiaRecords", "Votes", "RandomMessage");

        foreach ($redirect as $plugin) {
            $classObj = "MLEPP\\" . $plugin . "\\" . $plugin;
            $mlepp->ChatRedirection->registerRedirect($classObj, $this, 'sendMessage');
            $mlepp->ChatRedirection->setHideChat($classObj);
        }

        parent::onLoad();
    }

    public function onUnload() {
        parent::onUnload();
    }

    public function onReady() {


        parent::onReady();
    }

    public function onPlayerConnect($login, $isSpectator) {
        //$customUi = new CustomUI();
        //$customUi->checkpointList = false;
        //WindowHandler::setCustomUI($customUi, $this->storage->getPlayerObject($login));
        parent::onPlayerConnect($login, $isSpectator);
    }

    public function onPlayerDisconnect($login) {
        //The parent handles the widget we need to call it
        parent::onPlayerDisconnect($login);
    }

    public function sendMessage($message, $toLogin = false) {
        $this->updateWidgetData(array("fromLogin" => false, "toLogin" => $this->toString($toLogin), "message" => $message, "tab" => "general"));
        $this->forceUpdateWidget();
    }

    public function sendMessageToTab($fromLogin, $toLogin, $message, $tabID, $fromPlugin = false) {

        $this->updateWidgetData(array("fromLogin" => $this->toString($fromLogin), "toLogin" => $this->toString($toLogin), "message" => $message, "tab" => $tabID));
        $this->forceUpdateWidget();
    }

    public function toString($login) {
        $loginString = null;
        if ($login) {
            if (isset($login->login)) {
                $loginString = $login->login;
            } else {
                $loginString = $login;
            }
            return $loginString;
        }
    }

    function destroy() {
        parent::destroy();
        gc_collect_cycles();
    }

}

?>
